package com.molyfun.parents.activity.report;

import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.fragment.BaseFragment;
import com.molyfun.parents.activity.course.CourseFromsChooseActivity;
import com.molyfun.parents.rest.bean.RepoetFromsLineListResponse;
import com.molyfun.parents.rest.bean.ReportFromsLineCourseResponse;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.uievent.UIEventListener;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.utils.MPChartHelper;
import com.molyfun.parents.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/12/14 0014.
 */

public class CourseFragment extends BaseFragment implements View.OnClickListener, UIEventListener {

    @InjectView(R.id.tv_strtime)
    TextView tv_time;
    @InjectView(R.id.lineChart)
    LineChart lineChart;
    @InjectView(R.id.course_name)
    TextView course_name;
    @InjectView(R.id.aim_name)
    TextView aim_name;

    private List<String> xAxisValues;
    private List<Float> yAxisValues;
    private String mReportId;
    private ArrayList<RepoetFromsLineListResponse.LineListBean> targetList;

    public static CourseFragment newInstance(int category, String strTime, String reportId, ArrayList<RepoetFromsLineListResponse.LineListBean> lineList) {
        CourseFragment courseFragment = new CourseFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("category", category);
        bundle.putString(Const.FLAG_TIME_INTERVAL, strTime);
        bundle.putString(Const.REPORT_ID, reportId);
        bundle.putParcelableArrayList("targetList", lineList);
        courseFragment.setArguments(bundle);
        return courseFragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_report_course, null);
        ButterKnife.inject(this, view);
        initView();
        return view;
    }

    private void initView() {
        String mTimeInterval = getArguments().getString(Const.FLAG_TIME_INTERVAL);
        mReportId = getArguments().getString(Const.REPORT_ID);
        targetList = getArguments().getParcelableArrayList("targetList");
        if (mTimeInterval != null && !TextUtils.isEmpty(mTimeInterval)) {
            tv_time.setText(mTimeInterval + "");
        }
        if (targetList != null && targetList.size() > 0) {
            loadDate(mReportId, targetList.get(0).getCoursenamelist().get(0).getId());
            if(TextUtils.isEmpty(targetList.get(0).getCoursenamelist().get(0).getName())){
                course_name.setText("请点选课程");
            }else{
                course_name.setText(targetList.get(0).getCoursenamelist().get(0).getName());
            }
            aim_name.setText(targetList.get(0).getTarget());
        }
        course_name.setOnClickListener(this);
        MFPApplication.getInstance().getEventController().addUIEventListener(EventDispatcherEnum.UI_EVENT_REPORTFROMSCOURSE, this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.course_name:
                if (targetList != null && targetList.size() > 0) {

                    CourseFromsChooseActivity.startActivity(getContext(), "", mReportId, targetList);
//
//                    new CourseFromsChooseActivity.FromsDialogBuilder("", mReportId, targetList).build(getChildFragmentManager().beginTransaction());
                }
                break;
        }
    }

    private void initData(ArrayList<ReportFromsLineCourseResponse.CourseBean.ResultlistBean> resultlist) {
        xAxisValues = new ArrayList<>();
        yAxisValues = new ArrayList<>();
        for (int i = 0; i < resultlist.size(); i++) {
            xAxisValues.add(resultlist.get(i).getDate());
            yAxisValues.add(StringUtils.convertToFloat(resultlist.get(i).getResult(), 0));
        }
    }

    private void loadDate(String reportId, String targetId) {
        showProgressDialog();
        Call<ReportFromsLineCourseResponse> call = MFPApplication.getInstance().getRestClient().getReportLineCourse(reportId, targetId);
        call.enqueue(new Callback<ReportFromsLineCourseResponse>() {
            @Override
            public void onResponse(Call<ReportFromsLineCourseResponse> call, Response<ReportFromsLineCourseResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    if (response.body().getData().getResultlist().size() > 0) {
                        initData(response.body().getData().getResultlist());
                        lineChart.setScaleYEnabled(false);
                        MPChartHelper.setLineChart(lineChart, xAxisValues, yAxisValues, "折线图", true);
                    } else {
                        return;
                    }
                } else {
                    toast(response.body() == null ? "" : response.body().getMsg() == null ? "" : response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<ReportFromsLineCourseResponse> call, Throwable t) {
                dismissProgressDialog();
                toast("获取数据失败");
            }
        });
    }

    private long currentTime = 01;

    @Override
    public void handleUIEvent(Message msg) {
        if (msg.what == EventDispatcherEnum.UI_EVENT_REPORTFROMSCOURSE) {
            CourseFromsChooseActivity.MessgeBean bean = (CourseFromsChooseActivity.MessgeBean) msg.obj;

            if(TextUtils.isEmpty(bean.getCourserName())){
                course_name.setText("请点选课程");
            }else{
                course_name.setText(bean.getCourserName());
            }
            aim_name.setText(bean.getTargetName());
            if (currentTime == 01) {
                loadDate(mReportId, bean.getTagertId());
                currentTime = System.currentTimeMillis();
            } else {
                long newTime = System.currentTimeMillis();
                if (newTime - currentTime > 500) {
                    loadDate(mReportId, bean.getTagertId());
                    currentTime = newTime;
                }
            }
        }

    }
}
